package com.xbongbong.paas.service.dynamic.crm.strategy.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.PaasFormEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.log.constant.CrmLogMemoConstant;
import com.xbongbong.paas.pojo.dto.FormDataDeleteBatchDTO;
import com.xbongbong.paas.pojo.dto.FormDataUpdateDTO;
import com.xbongbong.paas.pojo.dto.SaasFormSaveDTO;
import com.xbongbong.paas.service.dynamic.crm.strategy.AbstractDynamicStrategy;
import com.xbongbong.parent.interfaces.DynamicStrategy;
import com.xbongbong.paas.service.dynamic.crm.strategy.MainBusinessDynamicStrategy;
import com.xbongbong.paas.service.dynamic.crm.strategy.PushNotifyDynamicStrategy;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.pro.customercommunicate.pojo.dto.DynamicDTO;
import com.xbongbong.pro.dynamic.pojo.dto.DynamicArchiveDTO;
import com.xbongbong.pro.dynamic.pojo.dto.DynamicCheckJoinTeamDTO;
import com.xbongbong.pro.dynamic.pojo.dto.DynamicDeleteDTO;
import com.xbongbong.pro.dynamic.pojo.dto.DynamicLabelDTO;
import com.xbongbong.pro.dynamic.pojo.dto.DynamicRefInfoDTO;
import com.xbongbong.pro.dynamic.pojo.dto.DynamicSaveDTO;
import com.xbongbong.pro.dynamic.pojo.dto.DynamicStageJumpDTO;
import com.xbongbong.pro.dynamic.pojo.dto.DynamicUpdateBatchDTO;
import com.xbongbong.pro.dynamic.pojo.dto.DynamicUpdateDTO;
import com.xbongbong.pro.formdata.pojo.vo.BeforeUpdateVO;
import com.xbongbong.pro.formdata.pojo.vo.UpdateVO;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.entity.ext.ContractEntityExt;
import com.xbongbong.saas.enums.ProjectLogEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.base.ContractBaseEnum;
import com.xbongbong.saas.enums.business.ContractEnum;
import com.xbongbong.saas.model.ContractModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *合同策略
 *
 *@author weijianxun
 */
@Service("contractDynamicStrategyImpl")
public class ContractDynamicStrategyImpl extends AbstractDynamicStrategy
        implements DynamicStrategy, PushNotifyDynamicStrategy , MainBusinessDynamicStrategy {

    private final Logger LOG = LoggerFactory.getLogger(ContractDynamicStrategyImpl.class);

    private static final Map<String, Integer> refTextToLogTypeMap = new HashMap<>();

    static {
        refTextToLogTypeMap.put(ContractEnum.LINK_CUSTOMER.getAttr(), ProjectLogEnum.CUSTOMER_REF.getSubType());
        refTextToLogTypeMap.put(ContractEnum.LINK_OPPORTUNITY.getAttr(), ProjectLogEnum.OPPORTUNITY_REF.getSubType());
        refTextToLogTypeMap.put(ContractEnum.LINK_CONTACT.getAttr(), ProjectLogEnum.CONTACT_REF.getSubType());
    }


    @Resource
    private ContractModel contractModel;

    public static Map<String, Integer> getRefTextToLogTypeMap(){
        return Collections.unmodifiableMap(refTextToLogTypeMap);
    }

    @Override
    public List<Integer> type() {
        return Arrays.asList(
                XbbRefTypeEnum.CONTRACT.getCode()
        );
    }

    @Override
    @Async("dynamicThreadPool")
    public void save(DynamicSaveDTO dynamicSaveDTO) {
        try {
            SaasFormSaveDTO saasFormSaveDTO = dynamicSaveDTO.getSaasFormSaveDTO();
            String corpid = saasFormSaveDTO.getCorpid();
            Long formId = dynamicSaveDTO.getFormId();
            Long dataId = dynamicSaveDTO.getDataId();
            String contractNo = saasFormSaveDTO.getNewPaasFormDataEntity().getSerialNo();
            PaasFormEntity paasForm = getPaasForm(corpid, formId, saasFormSaveDTO.getBusinessType());
            saveRefUpDynamic(dynamicSaveDTO, refTextToLogTypeMap, paasForm, DynamicRefInfoDTO.initDynamicRefInfoDTO(dataId, saasFormSaveDTO.getBusinessType(), contractNo), ProjectLogEnum.CONTRACT_CREATE.getSubType());
        } catch (Exception e) {
            LOG.error("com.xbongbong.paas.service.dynamic.crm.strategy.impl.ContractDynamicStrategyImpl.save", e);
        }
    }

    @Override
    @Async("dynamicThreadPool")
    public void update(DynamicUpdateDTO dynamicUpdateDTO) {
        try {
            FormDataUpdateDTO formDataUpdateDTO = dynamicUpdateDTO.getFormDataUpdateDTO();
            BeforeUpdateVO beforeUpdateVO = dynamicUpdateDTO.getBeforeUpdateVO();
            UpdateVO updateVO = dynamicUpdateDTO.getUpdateVO();
            PaasFormDataEntity paasFormDataEntity = beforeUpdateVO.getPaasFormDataEntity();
            PaasFormEntity paasForm = getPaasForm(formDataUpdateDTO.getCorpid(), paasFormDataEntity.getFormId(), formDataUpdateDTO.getBusinessType());
            DynamicDTO dynamicDTO = initDynamicDTO(updateVO.getUserVO().getUserId(), CrmLogMemoConstant.DYNAMIC_USER_UPDATE_BASE_INFO, ProjectLogEnum.CONTRACT_UPDATE.getSubType(),
                    DynamicDTO.initMemoHolder(updateVO.getUserVO().getName(), paasForm.getName()), paasForm, formDataUpdateDTO.getSaasMark(), paasFormDataEntity.getId());
            insertDynamic(Collections.singletonList(dynamicDTO), formDataUpdateDTO.getCorpid());
        } catch (Exception e) {
            LOG.error("com.xbongbong.paas.service.dynamic.crm.strategy.impl.ContractDynamicStrategyImpl.update", e);
        }
    }

    @Override
    @Async("dynamicThreadPool")
    public void batchUpdate(DynamicUpdateBatchDTO dynamicUpdateBatchDTO) {
        try {
            super.batchUpdate(dynamicUpdateBatchDTO, ProjectLogEnum.CONTRACT_UPDATE.getSubType());
        } catch (Exception e) {
            LOG.error("com.xbongbong.paas.service.dynamic.crm.strategy.impl.ContractDynamicStrategyImpl.batchUpdate", e);
        }
    }

    @Override
    @Async("dynamicThreadPool")
    public void batchDelete(DynamicDeleteDTO dynamicDeleteDTO) {
        try {
            FormDataDeleteBatchDTO formDataDeleteBatchDTO = dynamicDeleteDTO.getFormDataDeleteBatchDTO();
            List<Long> allowDeleteDataIds = dynamicDeleteDTO.getAllowDeleteDataIds();
            String corpid = formDataDeleteBatchDTO.getCorpid();
            Map<String, Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            params.put(StringConstant.CORPID, corpid);
            params.put(StringConstant.IDIN, allowDeleteDataIds);
            List<ContractEntityExt> contractEntityExts = contractModel.findEntitysWithoutSub(params);
            List<PaasFormDataEntityExt> paasFormDataEntityExts = new ArrayList<>(contractEntityExts.size());
            BeanUtil.copyPropertiesList(contractEntityExts, paasFormDataEntityExts, PaasFormDataEntityExt.class);
            dynamicDeleteDTO.setAllowDeletePaasFormDatas(paasFormDataEntityExts);
            PaasFormEntity paasForm = getPaasForm(corpid, formDataDeleteBatchDTO.getFormId(), formDataDeleteBatchDTO.getBusinessType());
            super.batchRefSaveUpDynamicByDeleteBatch(dynamicDeleteDTO, refTextToLogTypeMap, paasForm, ContractBaseEnum.CONTRACT_NO.getAttr(), ProjectLogEnum.CONTRACT_DELETE.getSubType());
        } catch (Exception e) {
            LOG.error("com.xbongbong.paas.service.dynamic.crm.strategy.impl.ContractDynamicStrategyImpl.batchDelete", e);
        }
    }

    @Override
    @Async("dynamicThreadPool")
    public void checkJoinTeam(DynamicCheckJoinTeamDTO dynamicCheckJoinTeamDTO) {
        try {
            super.checkJoinTeam(dynamicCheckJoinTeamDTO, ProjectLogEnum.CONTRACT_TEAM_ADD.getSubType());
        } catch (Exception e) {
            LOG.error("com.xbongbong.paas.service.dynamic.crm.strategy.impl.ContractDynamicStrategyImpl.checkJoinTeam", e);
        }
    }

    @Override
    @Async("dynamicThreadPool")
    public void archive(DynamicArchiveDTO dynamicArchiveDTO) {
        try {
            super.archive(dynamicArchiveDTO, ProjectLogEnum.CONTRACT_ARCHIVE.getSubType());
        } catch (Exception e) {
            LOG.error("com.xbongbong.paas.service.dynamic.crm.strategy.impl.ContractDynamicStrategyImpl.archive", e);
        }
    }

    @Override
    public void stageJump(DynamicStageJumpDTO dynamicStageJumpDTO) {

    }

    @Override
    @Async("dynamicThreadPool")
    public void label(DynamicLabelDTO dynamicLabelDTO) {
        try {
            super.label(dynamicLabelDTO, ProjectLogEnum.CONTRACT_UPDATE.getSubType());
        } catch (Exception e) {
            LOG.error("com.xbongbong.paas.service.dynamic.crm.strategy.impl.ContractDynamicStrategyImpl.label", e);
        }
    }
}
